﻿using System.Collections.Generic;
using UnityEngine;
using FairyGUI;

public class FWeaponListMediator : MyWindow {
    GList _list;
    FEditWeaponMediator _editWeaponPanel;
    FCardTypePopup _weaponTypePopup;
    FCardTypePopup _roleTypePopup;
    GComponent _weaponList;
    GTextField _weaponNum;

    static List<Weapon> _weaponDataList;//所有武器数据
    static List<Weapon> _curWeaponDataList;//筛选后的武器数据
    static FWeaponListMediator _instance;

    OneDelegate _callback;

    protected override void OnInit(){
        base.OnInit();
        this.contentPane = UIPackage.CreateObject("EquipmentPackage", "WeaponList").asCom;
        _weaponList = this.contentPane;
        InitPanel();
    }

    public FWeaponListMediator(OneDelegate cb = default(OneDelegate))
    {
        _instance = this;

        SetCallBack(cb);
    }

    public void SetCallBack(OneDelegate cb)
    {
        _callback = cb;
        FreshCallBackPanel();
    }

    private void FreshCallBackPanel()
    {
        if (_weaponList == null) return;
        var closeBtn = _weaponList.GetChild("closeBtn");
        if (_callback != null)
        {
            closeBtn.onClick.Add(OnCloseBtn);
            closeBtn.visible = true;
        }
        else
        {
            closeBtn.visible = false;
        }
    }

    private void InitPanel()
    {
        FreshCallBackPanel();

        _weaponNum = _weaponList.GetChild("numText").asTextField;
        _weaponList.GetChild("newBtn").onClick.Add(OnNewBtnClick);
        var weaponTypeBtn = _weaponList.GetChild("weaponTypeBtn");
        var roleTypeBtn = _weaponList.GetChild("roleTypeBtn");
        weaponTypeBtn.onClick.Add(OnTypeBtnClick);
        roleTypeBtn.onClick.Add(OnTypeBtnClick);
        weaponTypeBtn.visible = true;
        roleTypeBtn.visible = true;
        _list = _weaponList.GetChild("weaponList").asList;
        //编辑武器面板
        _editWeaponPanel = (FEditWeaponMediator)FUIMain.GetWindow(GMConfig.PanelType.EditWeaponPanel,null);
        //武器类型选择
        UIObjectFactory.SetPackageItemExtension("ui://CardPackage/CardTypePopup", typeof(FCardTypePopup));
        _weaponTypePopup = (FCardTypePopup)UIPackage.CreateObject("CardPackage", "CardTypePopup");
        _roleTypePopup = (FCardTypePopup)UIPackage.CreateObject("CardPackage", "CardTypePopup");

        InitData();
    }
    private static void InitData()
    {
        if (_weaponDataList != null) return;
        _weaponDataList = WeaponMO.GetWeaponList();
    }
    // Use this for initialization
    protected override void OnShown()
    {         
        //设置item单机事件监听
        _list.onClickItem.Add(OnWeaponListClick);
        _list.itemRenderer = RenderListItem;

        //赋值
        var typeDict = _editWeaponPanel.GetTypeDict();
        _weaponTypePopup.InitData(typeDict.GetValue("typeTextList"),FreshWeaponList);
        _roleTypePopup.InitData(typeDict.GetValue("roleTextList"), FreshWeaponList);

        FreshWeaponList();
    }

    void RenderListItem(int index, GObject item)
    {
        var btn = (FWeapon)item;
        btn.InitData(_curWeaponDataList[index]);
    }

    private void OnWeaponListClick(EventContext context)
    {
        var index = _list.GetChildIndex((GObject)context.data);
        if(_callback != null)
        {
            _callback(_curWeaponDataList[index].id);
        }
        else
        {
            _editWeaponPanel.Show();
            //获取对应所有武器内的下标，以便后面保存
            var _index = WeaponMO.GetWeaponIndex(_curWeaponDataList[index]);
            _editWeaponPanel.InitData(_curWeaponDataList[index], _index);
        }
    }
    /// <summary>
    /// 新建武器
    /// </summary>
    private void OnNewBtnClick()
    {
        //创建一张新卡
        var _weapon = new Weapon();
        int lastId = 0;
        if (_weaponDataList.Count != 0)
        {
            var lastCard = _weaponDataList[_weaponDataList.Count - 1];
            lastId = System.Convert.ToInt32(lastCard.id.Split('_')[1]);
        }
        _weapon.id = "Weapon_" + (lastId + 1);
        WeaponMO.ResetWeaponName(_weapon);
        _editWeaponPanel.Show();
        _editWeaponPanel.InitData(_weapon,_weaponDataList.Count);
    }
    /// <summary>
    /// 类型筛选
    /// </summary>
    /// <param name="context"></param>
    private void OnTypeBtnClick(EventContext context)
    {
        var sender = (GComponent)context.sender;

        switch (sender.name)
        {
            case "weaponTypeBtn":
                GRoot.inst.ShowPopup(_weaponTypePopup, sender);
                break;
            case "roleTypeBtn":
                GRoot.inst.ShowPopup(_roleTypePopup, sender);
                break;
        }
    }
    /// <summary>
    /// 保存武器
    /// </summary>
    /// <param name="weaponData"></param>
    /// <param name="index"></param>
    /// <returns></returns>
    public static bool SaveWeapon(Weapon weaponData,int index)
    {
        WeaponMO.SaveWeapon(weaponData,index);
        if(_instance != null) _instance.FreshWeaponList();
        return true;
    }
    /// <summary>
    /// 删除武器
    /// </summary>
    /// <param name="index"></param>
    /// <returns></returns>
    public static bool DelWeapon(int index){
        if (index < 0 || index >= _weaponDataList.Count) return false;
        WeaponMO.DelWeapon(index);
        if (_instance != null) _instance.FreshWeaponList();
        return true;
    }
    /// <summary>
    /// 刷新武器面板
    /// </summary>
    private void FreshWeaponList(){
        var weaponTypeSelectedList = _weaponTypePopup.GetSelectedList();
        var roleTypeSelectedList = _roleTypePopup.GetSelectedList();

        _curWeaponDataList = new List<Weapon>();

        for(int j = 0;j < _weaponDataList.Count; j++)
        {
            //武器类型筛选
            var weaponTypeOk = weaponTypeSelectedList.Count == 0 || weaponTypeSelectedList.Contains((int)_weaponDataList[j].type);
            var roleTypeOk = roleTypeSelectedList.Count == 0;
            //角色类型筛选
            for (int i = 0;i < _weaponDataList[j].roles.Count; i++)
            {
                if (roleTypeSelectedList.Contains((int)_weaponDataList[j].roles[i]))
                {
                    roleTypeOk = true;
                    break;
                }
            }
            if (weaponTypeOk && roleTypeOk) _curWeaponDataList.Add(_weaponDataList[j]);
        }
        //全部都没选择
        if(weaponTypeSelectedList.Count == 0 && roleTypeSelectedList.Count == 0)
        {
            _curWeaponDataList = _weaponDataList;
        }
        // _list.RemoveChildrenToPool();
        // _list.SetVirtual();
        _list.numItems = _curWeaponDataList.Count;
        //数量统计
        _weaponNum.text = _curWeaponDataList.Count.ToString();
    }
	// Update is called once per frame
	void Update () {
		
	}
    /// <summary>
    /// 关闭面板
    /// </summary>
    private void OnCloseBtn()
    {
        if (_callback != null) _callback(null);
    }
}
